﻿
using Base.ClientUI;
using System.Windows.Forms;
using Base.Common;
using System.Collections.Generic;
using System.ComponentModel;
 namespace Base.ClientUI {
    public partial class StatTaskFrms : UITitlePage
    {
        public StatTaskFrms()
        {
            InitializeComponent();
            InitGridColumn();
         }
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ExStyle |= 0x02000000; // 用双缓冲绘制窗口的所有子控件
                return cp;
            }
        }
        public override void Init()
        {
            base.Init();
            PageGridSource();
        }
         private bool PageGridSource()
        {
            List<TaskStat> list = HttpSysUtil.getStat();
            if (list == null) {
                this.ShowErrorTip("网络不稳定!");
                return false;
            }
            long all = 1;
            foreach( TaskStat ts in list) {
                all += ts.workAllTime;
            }
            foreach( TaskStat ts in list ) {
                 ts.ppm= (int)(ts.workAllTime*100/all);
            }
           
            uiDataGridView1.DataSource = list;
             return true;
         }
         private void InitGridColumn()
        {
            uiDataGridView1.AddColumn("名称", "className").AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            uiDataGridView1.AddColumn("间隔", "time").AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            uiDataGridView1.AddColumn("次数", "workCount").AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            uiDataGridView1.AddColumn("占比" ,"ppm").AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            uiDataGridView1.AddColumn("执行总时间", "workAllTime").AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            uiDataGridView1.AddColumn("最近执行时间", "worklastTime").AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            uiDataGridView1.AddColumn("最长时间", "maxTime").AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            uiDataGridView1.AddColumn("最短时间", "minTime").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            uiDataGridView1.AddColumn("平均", "avg").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            uiDataGridView1.AddColumn("异常", "exceptionCount").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            uiDataGridView1.ReadOnly = true;
         
        }
        
       
        private void uiDataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            string name = uiDataGridView1.Columns[e.ColumnIndex].DataPropertyName;
            if ("avg" == name)
            {
                DataGridViewRow row = uiDataGridView1.Rows[e.RowIndex];
                TaskStat stat = (TaskStat)row.DataBoundItem;
                if (stat.workCount != 0)
                {
                    e.Value = stat.workAllTime / stat.workCount;
                }
            }

            if ("workAllTime" == name || "worklastTime" == name || "maxTime" == name || "minTime" == name||"avg" == name) {
                DataGridViewRow row = uiDataGridView1.Rows[e.RowIndex];
                TaskStat stat = (TaskStat)row.DataBoundItem;
                if (e.Value == null) {
                    return;
                }
                var v = long.Parse(e.Value.ToString());
                 if (v < 10) {
                    e.Value = v * 100 + "ns";
                }else  if (v < 10000) {
                    e.Value = v/10 + "us";
                }else if (v < 10000*1000) {
                    e.Value = v /10000 + "ms";
                } else  {
                    e.Value = v / 10000/1000 + "s";
                } 

                
            }
        }
         private void uiDataGridView1_MouseUp(object sender ,MouseEventArgs e) {
            if( e.Button == MouseButtons.Right ) {
                if( PageGridSource() ) {
                    this.ShowInfoTip("已加载！");
                }
            }
         }
    }
}